package com.sinosoft.cnmuseum.servlets;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sinosoft.cnmuseum.dao.MbManageAudiofrequencyDao;
import com.sinosoft.cnmuseum.dao.MbManageImageDao;
import com.sinosoft.cnmuseum.dao.MbManageLiteratureDao;
import com.sinosoft.cnmuseum.dao.MbManageThreedimensionalmodelDao;
import com.sinosoft.cnmuseum.dao.MbManageVideoDao;
import com.sinosoft.cnmuseum.dao.MbMusicDbrwDao;
import com.sinosoft.cnmuseum.dao.MbMusicWhsxDao;
import com.sinosoft.cnmuseum.entity.MbGyHsfl;
import com.sinosoft.cnmuseum.entity.MbGyMjfl;
import com.sinosoft.cnmuseum.entity.MbGyYqfl;
import com.sinosoft.cnmuseum.entity.MbGyYqzl;
import com.sinosoft.cnmuseum.entity.MbManageAudiofrequency;
import com.sinosoft.cnmuseum.entity.MbManageImage;
import com.sinosoft.cnmuseum.entity.MbManageLiterature;
import com.sinosoft.cnmuseum.entity.MbManageThreedimensionalmodel;
import com.sinosoft.cnmuseum.entity.MbManageVideo;
import com.sinosoft.cnmuseum.entity.MbMusicWhsx;
import com.sinosoft.cnmuseum.utils.Status;
import com.sinosoft.cnmuseum.utils.StringUtils;
import com.sinosoft.commontools.annotated.TransAnnotation;
import com.sinosoft.commontools.annotated.rquestMethod;
import com.sinosoft.commontools.annotated.rquestMethod.ReqMode;
import com.sinosoft.commontools.record.Record;

public class MbWhsxServlet {

  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record getWhsxListByGcxxId(HttpServletRequest request, HttpServletResponse response, Record data){
    Record result = new Record();
    String gcxxid = data.getStrValue("gcxxid");
    if(StringUtils.isEmpty(gcxxid))
      result.add(Status.SATUS, Status.FAILED);
    else{
      List<Record> list = getWhsxListByGcxxId(Integer.valueOf(gcxxid));
      

      String[] baseTables = {MbGyYqzl.MB_GY_YQZL_TABLE_NAME};
      String[] fields = {"instrumentType"};
      MbXtglBaseTableServlet baseTableServlet = new MbXtglBaseTableServlet();
      for(int i=0;i<fields.length;i++){
        String f=fields[i];
        for(int j=0;j<list.size();j++){
          Record d = list.get(j);
          String value = d.getStrValue(f);
          String text = "";
          if(StringUtils.isNoEmpty(value)){
            text = baseTableServlet.getNameById(baseTables[i],Integer.valueOf(value));
          }
          d.add(f+"_text", text);
        }
      }
      
      result.add(Status.SATUS, Status.OK);
      result.add(Status.ROWS, list);
    }
    return result;
    
  }

  public static List<Record> getWhsxListByGcxxId(int gcxxid) {
    String sql = "select * from "+MbMusicWhsx.MB_MUSIC_WHSX_TABLE_NAME +" where gcxxid="+gcxxid;
    MbMusicWhsxDao dao = new MbMusicWhsxDao();
    List list = dao.getBySql(sql);
    return list;    
  }
  
  

  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record getWhsxDetailByZdbh(HttpServletRequest request, HttpServletResponse response, Record data){
    Record result = new Record();
    String zdbhStr = data.getStrValue("zdbh");
    if(StringUtils.isEmpty(zdbhStr))
      result.add(Status.SATUS, Status.FAILED);
    else{
      MbMusicWhsxDao dao = new MbMusicWhsxDao();
      Record whsx = dao.getById(Integer.valueOf(zdbhStr));
      


      String[] imagesByZdbh = new String[]{"musicScoreImage"};
      String[] videoByZdbh = new String[]{"inheritTeachVideo"};
      
      MbManageImageDao imagedao = new MbManageImageDao();
      for (String resource : imagesByZdbh) {
        List<Record> ls = imagedao.getBySql("select * from "+MbManageImage.MB_MANAGE_IMAGE_TABLE_NAME
            +" where zdbh='" + whsx.getStrValue(resource) + "'");
        if (!ls.isEmpty()) {
          whsx.add(resource + "1", ls.get(0).getStrValue("title"));
        }
      }
      MbManageVideoDao videodao = new MbManageVideoDao();
      for (String resource : videoByZdbh) {
        List<Record> ls = videodao.getBySql("select * from "+MbManageVideo.MB_MANAGE_VIDEO_TABLE_NAME
            +" where zdbh='" 
      + whsx.getStrValue(resource) + "'");
        if (!ls.isEmpty()) {
          whsx.add(resource + "1", ls.get(0).getStrValue("title"));
        }
      }
      
      
      
      result.add(Status.SATUS, Status.OK);
      result.add(Status.ROWS,whsx);
    }
    return result;
  }
  

  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record saveWhsx(HttpServletRequest request, HttpServletResponse response, Record data){
    Record result = new Record();
    MbMusicWhsxDao dao = new MbMusicWhsxDao();
    String zdbhStr = data.getStrValue("whsx_zdbh");
    if(StringUtils.isEmpty(zdbhStr)){
      Record r = new Record();
      r.add("instrumentType", data.getStrValue("instrumentType"));
      r.add("songster", data.getStrValue("songster"));
      r.add("lyricist", data.getStrValue("lyricist"));
      r.add("performingForms", data.getStrValue("performingForms"));
      r.add("playForm", data.getStrValue("playForm"));
      r.add("performer", data.getStrValue("performer"));
      r.add("genre", data.getStrValue("genre"));
      r.add("toPlayClub", data.getStrValue("toPlayClub"));
      r.add("musicScoreExplain", data.getStrValue("musicScoreExplain"));
      r.add("musicScoreImage", data.getStrValue("musicScoreImage"));
      r.add("inheritTeachExplain", data.getStrValue("inheritTeachExplain"));
      r.add("inheritTeachVideo", data.getStrValue("inheritTeachVideo"));
      r.add("symbolsAndTaboos", data.getStrValue("symbolsAndTaboos"));
      r.add("socialFunction", data.getStrValue("socialFunction"));
      r.add("culturalRole", data.getStrValue("culturalRole"));
      r.add("culturalFunction", data.getStrValue("culturalFunction"));
      r.add("culturalContext", data.getStrValue("culturalContext"));
      r.add("gcxxid", data.getStrValue("gcxxid"));
      dao.insert(r);
      result.add(Status.SATUS, Status.OK);
    }
    else{
      Record r = dao.getById(Integer.valueOf(zdbhStr));
      r.add("instrumentType", data.getStrValue("instrumentType"));
      r.add("songster", data.getStrValue("songster"));
      r.add("lyricist", data.getStrValue("lyricist"));
      r.add("performingForms", data.getStrValue("performingForms"));
      r.add("playForm", data.getStrValue("playForm"));
      r.add("performer", data.getStrValue("performer"));
      r.add("genre", data.getStrValue("genre"));
      r.add("toPlayClub", data.getStrValue("toPlayClub"));
      r.add("musicScoreExplain", data.getStrValue("musicScoreExplain"));
      r.add("musicScoreImage", data.getStrValue("musicScoreImage"));
      r.add("inheritTeachExplain", data.getStrValue("inheritTeachExplain"));
      r.add("inheritTeachVideo", data.getStrValue("inheritTeachVideo"));
      r.add("symbolsAndTaboos", data.getStrValue("symbolsAndTaboos"));
      r.add("socialFunction", data.getStrValue("socialFunction"));
      r.add("culturalRole", data.getStrValue("culturalRole"));
      r.add("culturalFunction", data.getStrValue("culturalFunction"));
      r.add("culturalContext", data.getStrValue("culturalContext"));
      r.add("gcxxid", data.getStrValue("gcxxid"));
      dao.update(r);
      result.add(Status.SATUS, Status.OK);
    }
    return result;
  }
  

  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record delWhsx(HttpServletRequest request, HttpServletResponse response, Record data){
    Record result = new Record();
    MbMusicWhsxDao dao = new MbMusicWhsxDao();
    String zdbhStr = data.getStrValue("zdbh");
    if(StringUtils.isEmpty(zdbhStr)){
      result.add(Status.SATUS, Status.FAILED);
    }else{
      dao.delete(dao.getById(Integer.valueOf(zdbhStr)));
    }
    return result;
  }
}
